{{define "export-importer"}}
{{template "top" .}}

{{$model := .model}}
{{$keys := .keys}}

<div class="card shadow-sm">
  <div class="card-header">
    {{if .model.ID}}
      Edit Export Importer Config {{.model.ID}}
    {{else}}
      New Export Importer Config
    {{end}}
  </div>

  <div class="card-body">
    {{if .model.ID}}
      <div class="alert alert-warning" role="alert">
        You are editing an export importer config. Any changes will only impact
        future batches.
      </div>
      <div class="alert alert-info" role="alert">
        Export importer configurations cannot be deleted. Instead they are
        expired by setting and end date and time.
      </div>
    {{end}}

    <form method="POST" action="/export-importers/{{.model.ID}}" class="m-0 p-0">
      <div class="row g-3">
        <div class="col-12">
          <div class="form-floating">
            <input type="text" name="index-file" id="index-file" value="{{.model.IndexFile}}"
              placeholder="Index file" class="form-control font-monospace">
            <label for="index-file" class="form-label">Index file</label>
          </div>
          <div class="form-text text-muted">
            Absolute URL of the index.txt file containing the listing of export
            zip files.
          </div>
        </div>

        <div class="col-12">
          <div class="form-floating">
            <input type="text" name="export-root" id="export-root" value="{{.model.ExportRoot}}"
              placeholder="Export root" class="form-control font-monospace">
            <label for="export-root" class="form-label">Export root</label>
          </div>
          <div class="form-text text-muted">
            Common prefix for the relative paths of zip files contained in the
            index.
          </div>
        </div>

        <div class="col-12">
          <div class="form-floating">
            <input type="text" name="region" id="region" value="{{.model.Region}}"
              placeholder="Region" class="form-control font-monospace">
            <label for="region" class="form-label">Region</label>
          </div>
          <div class="form-text text-muted">
            Region to use when inserting the imported TEKs into this server's
            database.
          </div>
        </div>

        <div class="col-12">
          <div class="form-floating">
            <select name="travelers" id="travelers" class="form-select">
              <option value="true" {{if .model.Traveler}}selected{{end}}>Yes</option>
              <option value="false" {{if not .model.Traveler}}selected{{end}}>No</option>
            </select>
            <label for="travelers" class="form-label">Mark as travelers</label>
          </div>
          <div class="form-text text-muted">
            If 'yes', then all keys imported from this set of export files will be marked
            as traveler keys.
          </div>
        </div>

        <div class="col-12">
          <div class="input-group">
            <div class="form-floating">
              <input type="date" name="from-date" id="from-date" value="{{.model.From | htmlDate}}"
                min="2020-05-01" max="2029-12-21" class="form-control" />
              <label for="from-date" class="form-label">Valid from date</label>
            </div>
            <div class="form-floating">
              <input type="time" name="from-time" id="from-time" value="{{.model.From | htmlTime}}"
                class="form-control" />
              <label for="from-time" class="form-label">Valid from time</label>
            </div>
            <a href="https://www.timeanddate.com/worldclock/timezone/utc" target="_BLANK"
              class="input-group-text text-decoration-none">UTC</a>
          </div>
          <div class="form-text text-muted">
            Time when the exporter importer should start.
          </div>
        </div>

        <div class="col-12">
          <div class="input-group">
            <div class="form-floating">
              <input type="date" name="thru-date" id="thru-date" value="{{.model.Thru | htmlDate}}"
                min="2020-05-01" max="2029-12-21" class="form-control" />
              <label for="thru-date" class="form-label">Valid thru date</label>
            </div>
            <div class="form-floating">
              <input type="time" name="thru-time" id="thru-time" value="{{.model.Thru | htmlTime}}"
                class="form-control" />
              <label for="thru-time" class="form-label">Valid thru time</label>
            </div>
            <a href="https://www.timeanddate.com/worldclock/timezone/utc" target="_BLANK"
              class="input-group-text text-decoration-none">UTC</a>
          </div>
          <div class="form-text text-muted">
            Import other exports until this time. Normally left blank at the
            start, can be set to stop importing an export.
          </div>
        </div>

        <div class="d-grid col-12">
          <button type="submit" class="btn btn-primary" value="save">Save changes</button>
        </div>
      </div>
    </form>
  </div>
</div>

{{if $model.ID}}
<div class="card shadow-sm mt-3">
  <div class="card-header">
    Public Keys
  </div>
  {{if $keys}}
    <ul class="list-group list-group-flush">
      {{range $keys}}
        <li class="list-group-item py-3">
          <div class="row g-3">
            <div class="col-10">
              <strong>ID:</strong> {{.KeyID}}<br/>
              <strong>Version:</strong> {{.KeyVersion}}
              {{with $t := .From | htmlDatetime}}
                <br />
                <strong>Start:</strong> {{$t}}
              {{end}}
              {{with $t := .Thru | htmlDatetime}}
                <br />
                <strong>End:</strong> {{$t}}
              {{end}}
            </div>

            <div class="col-2 clearfix">
              <div class="float-end">
                {{if .Active}}
                  <span class="badge bg-success">Current</span>
                {{else if .Future}}
                  <span class="badge bg-info">Future</span>
                {{else}}
                  <span class="badge bg-warning">Not active</span>
                {{end}}
              </div>
            </div>

            <div class="col-12">
              <pre class="font-monospace small user-select-all bg-light border rounded p-3 mb-0">{{.PublicKeyPEM}}</pre>
            </div>

            <div class="col-12 clearfix">
              <div class="float-end">
                {{if .Future}}
                  <form method="POST" action="/export-importers-key/{{$model.ID}}/activate/{{.KeyID}}" class="m-0 p-0">
                    <button type="submit" class="btn btn-primary">Activate</button>
                  </form>
                {{else if .Active}}
                  <form method="POST" action="/export-importers-key/{{$model.ID}}/revoke/{{.KeyID}}" class="mb-0 p-0">
                    <button type="submit" class="btn btn-danger">Revoke</button>
                  </form>
                {{else if .Thru}}
                  <form method="POST" action="/export-importers-key/{{$model.ID}}/reinstate/{{.KeyID}}" class="mb-0 p-0">
                    <button type="submit" class="btn btn-warning">Clear Expiry Time</button>
                  </form>
                {{end}}
              </div>
            </div>
          </div>
        </li>
      {{end}}
    </ul>
  {{else}}
    <div class="card-body">
      <div class="alert alert-danger m-0">
        There are no public keys configured, so the export files cannot be imported.
      </div>
    </div>
  {{end}}
</div>

<div class="card shadow-sm mt-3">
  <div class="card-header">
    Create new public key for this import.
  </div>

  <div class="card-body">
    <form method="POST" action="/export-importers-key/{{.model.ID}}/create/new" class="m-0 p-0">
      <div class="row g-3">
        <div class="col-12">
          <div class="form-floating">
            <input type="text" name="keyid" id="keyid" value="{{.newkey.KeyID}}"
              placeholder="keyid" class="form-control font-monospace">
            <label for="keyid" class="form-label">Key ID</label>
          </div>
        </div>

        <div class="col-12">
          <div class="form-floating">
            <input type="text" name="version" id="version" value="{{.newkey.KeyVersion}}"
              placeholder="version" class="form-control font-monospace">
            <label for="version" class="form-label">Key Version</label>
          </div>
        </div>

        <div class="col-12">
          <div class="form-floating">
            <textarea name="public-key-pem" id="public-key-pem" placeholder="Public key PEM"
              class="form-control font-monospace" style="height:100px;">{{.newkey.PublicKeyPEM}}</textarea>
            <label for="public-key-pem" class="form-label">Public key PEM</label>
          </div>
          <div class="form-text text-muted">
            ECDSA p256 Public Key in
            <a href="https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail"
            target="_blank">PEM</a> format.
          </div>
        </div>

        <div class="col-12">
          <div class="input-group">
            <div class="form-floating">
              <input type="date" name="from-date" id="from-date" value="{{.newkey.From | htmlDate}}"
                min="2020-05-01" max="2029-12-21" class="form-control" />
              <label for="from-date" class="form-label">Start date</label>
            </div>
            <div class="form-floating">
              <input type="time" name="from-time" id="from-time" value="{{.newkey.From | htmlTime}}"
                class="form-control" />
              <label for="from-time" class="form-label">Start time</label>
            </div>
            <a href="https://www.timeanddate.com/worldclock/timezone/utc" target="_BLANK"
              class="input-group-text text-decoration-none">UTC</a>
          </div>
          <div class="form-text text-muted">
            Adjust if future effective time is needed.
          </div>
        </div>

        <div class="col-12">
          <div class="input-group">
            <div class="form-floating">
              <input type="date" name="thru-date" id="thru-date" value="{{.newkey.Thru | htmlDate}}"
                min="2020-05-01" max="2029-12-21" class="form-control" />
              <label for="thru-date" class="form-label">End date</label>
            </div>
            <div class="form-floating">
              <input type="time" name="thru-time" id="thru-time" value="{{.newkey.Thru | htmlTime}}"
                class="form-control" />
              <label for="thru-time" class="form-label">End time</label>
            </div>
            <a href="https://www.timeanddate.com/worldclock/timezone/utc" target="_BLANK"
              class="input-group-text text-decoration-none">UTC</a>
          </div>
          <div class="form-text text-muted">
            Leave blank if this is the current key version.
          </div>
        </div>

        <div class="d-grid col-12">
          <button type="submit" class="btn btn-primary" value="save">Create key</button>
        </div>
      </div>
    </form>
  </div>
</div>
{{end}}

{{template "bottom" .}}
{{end}}
